package com.xiaodou.mjf.web.console.basic.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.xiaodou.mjf.application.basic.service.OperatorService;
import com.xiaodou.mjf.application.core.repository.mybatis.InvestMapper;
import com.xiaodou.mjf.application.core.service.FinancingService;
import com.xiaodou.mjf.application.core.service.InvestService;
import com.xiaodou.mjf.entity.core.Financing;
import com.xiaodou.mjf.entity.core.Invest;
import com.xiaodou.mjf.infrastructure.common.controller.ApplicationController;
import com.xiaodou.mjf.infrastructure.common.utils.DateUtils;

@Controller
public class HomeController extends ApplicationController {

	@Resource
	OperatorService operatorService;
	@Resource
	FinancingService financingService;
	@Resource
	InvestService investService;
	@Resource
	InvestMapper investMapper;

	@RequestMapping("/")
	public String home(Model model) {
		try {
			List<Invest> investLists = Lists.newArrayList();
			List<Invest> financingLists = Lists.newArrayList();
			List<Invest> meiliLists = Lists.newArrayList();
			List<Invest> xiaodouLists = Lists.newArrayList();

			investLists = investMapper.getCusByProTotal();
			Object[][] financing = financingService.productTotal();

			if (financing.length > 0) {
				for (Object[] object : financing) {
					Invest entity = new Invest();
					entity.setCount(Integer.valueOf(object[1] == null ? "0" : object[1].toString()));
					entity.setType(Integer.valueOf(object[0] == null ? "0" : object[0].toString()));
					financingLists.add(entity);
				}
			}

			Calendar calendar = Calendar.getInstance();
			calendar.setTime(DateUtils.getCurrentTime());
			for (int i = 0; i < 7; i++) {

				Invest entity = new Invest();
				Invest entity2 = new Invest();
				// 判断是否第一天
				int firstDay = calendar.get(Calendar.DATE) - 1;
				if (firstDay == 0) {
					// 获取上月的最后一天
					Calendar cal = Calendar.getInstance();
					cal.setTime(DateUtils.getCurrentTime());
					cal.add(Calendar.MONTH, -1);
					int MaxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
					entity.setTerm(MaxDay);
					entity.setDeposit(cal.get(Calendar.MONTH) + 1);
					entity2.setTerm(MaxDay);
					entity2.setDeposit(cal.get(Calendar.MONTH) + 1);
				} else {
					entity.setTerm(calendar.get(Calendar.DATE) - 1);
					entity2.setTerm(calendar.get(Calendar.DATE) - 1);
					entity.setDeposit(calendar.get(Calendar.MONTH) + 1);
					entity2.setDeposit(calendar.get(Calendar.MONTH) + 1);
				}
				entity.setCount(0);
				entity2.setCount(0);
				calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) - 1);
				SimpleDateFormat spf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String a = spf.format(calendar.getTime());
				String startTimeStr = a.toString().substring(0, 10) + " 00:00:00";
				Date startTime = spf.parse(startTimeStr);

				String endTimeStr = a.toString().substring(0, 10) + " 23:59:59";
				Date endTime = spf.parse(endTimeStr);
				Object[][] object1 = investService.proBydateTotal(Financing.TYPE_MEILI, startTime, endTime);
				Object[][] object2 = investService.proBydateTotal(Financing.TYPE_XIAODOU, startTime, endTime);

				if (object1.length > 0) {
					for (Object[] object : object1) {
						entity.setCount(Integer.valueOf(object[1] == null ? "0" : object[1].toString()));
					}

				}

				if (object2.length > 0) {
					for (Object[] object : object2) {
						entity2.setCount(Integer.valueOf(object[1] == null ? "0" : object[1].toString()));
					}
				}

				meiliLists.add(entity);
				xiaodouLists.add(entity2);

			}

			model.addAttribute("meiliLists", meiliLists);
			model.addAttribute("xiaodouLists", xiaodouLists);
			model.addAttribute("investLists", investLists);
			model.addAttribute("financingLists", financingLists);

		} catch (Exception ex) {
			logger.error(ex.toString());
		}

		return "index";
	}

	@RequiresPermissions("view")
	@RequestMapping("view")
	@ResponseBody
	public String view() {
		return "ok";
	}
}
